package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.response.ResponseData;
import com.ruoyi.business.service.BasePayment;
import com.ruoyi.common.utils.HttpResponse;
import com.ruoyi.common.utils.HttpUtils;
import com.ruoyi.common.utils.MoneyUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;

@Service("xyfPayService")
public class XyfPayServiceImpl extends BasePayment {



    @Override
    public JSONObject createPayOrder(TOrderInfo orderInfo) throws Exception {
        JSONObject jsonObject = new JSONObject();
        TRecipientInfo userRecipientInfo = userRecipientService.selectTRecipientInfoById(orderInfo.getRecipientId());
        HashMap<String, String> headerMap = new HashMap();
        HashMap<String, String> dataMap = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        headerMap.put("User-Agent","Nebula AlipayDefined(nt:WIFI,ws:360|0|3.0) AliApp(AP/10.0.18.062203) AlipayClient/10.0.18.062203 Language/zh-Hans useStatusBar/true");
//
//        String url = "https://api.umvtech.com/api/payment/getUserName";
//        dataMap.put("user_code",userRecipientInfo.getPhoneNo());
//        HttpResponse priceHttpResponse = HttpUtils.httpFormPost(url,headerMap,"",dataMap,"kiss查询用户请求","");
//        JSONObject resultObj = JSONObject.parseObject(priceHttpResponse.getDocument().body().text());
//        int resultCode = resultObj.getInteger("status");
//        String priceId = "";
//        if(resultCode==1){
//
//
//
//        }

        String qrcode = userRecipientInfo.getReceiptQrcode();
        String qrcode_ =  qrcode.substring(qrcode.lastIndexOf("/")+1,qrcode.length());
        logger.info("qrcode："+qrcode_);
        String url ="https://yyfweb.postar.cn/postalpay/p/pu?state=SWPAY&qrcode="+qrcode_+"&userId="+orderInfo.getAliUserId();
        logger.info("收银台HTMLurl："+url);

        HttpResponse httpResponse = HttpUtils.httpFormGet(url,headerMap,"","星驿付");
        logger.info("返回的HTML:"+httpResponse.getDocument().text());

        Elements priceElements =  httpResponse.getDocument().select("div[class~=^APP_ID hide]");
        Element element1 = priceElements.get(0);
        String appId = element1.text();
        priceElements =  httpResponse.getDocument().select("div[class~=^CUST_ID hide]");
        element1 = priceElements.get(0);
        String cust_id = element1.text();
        priceElements =  httpResponse.getDocument().select("div[class~=^PLATFORM hide]");
        element1 = priceElements.get(0);
        String PLATFORM = element1.text();
        priceElements =  httpResponse.getDocument().select("div[class~=^PLATFORM hide]");
        element1 = priceElements.get(0);
        String sellerPercent = element1.text();

        url = "https://yyfweb.postar.cn/postalpay/p/getAdvert?custId="+cust_id;
        logger.info("getAdvertURL："+url);

        httpResponse = HttpUtils.httpFormGet(url,headerMap,"","星驿付");
        logger.info("星驿付请求getAdvert返回的:"+httpResponse.getBody());

        JSONObject resultObj = JSONObject.parseObject(httpResponse.getDocument().body().text());
        if(!resultObj.getString("RSPCOD").equals("00000")){
            jsonObject.put("code","100");
            jsonObject.put("msg","获取连接失败");
            return jsonObject;
        }
        String  ad_id = resultObj.getString("AD_ID");
        JSONObject payInfoObj = new JSONObject();
        payInfoObj.put("CUST_ID",cust_id);
        payInfoObj.put("adviseId",ad_id);
        payInfoObj.put("qrcode",qrcode_);
        orderInfo.setQrcodeTxt(payInfoObj.toJSONString());
        orderInfoService.updateTOrderInfo(orderInfo);

        dataMap = new HashMap();
        dataMap.put("sellerPercent",sellerPercent);
        dataMap.put("numOfInstallments","");
        dataMap.put("canUseHuaBei","1");
        dataMap.put("canUseDj","1");
        dataMap.put("qrcode",qrcode_);
        dataMap.put("app_id",appId);
        dataMap.put("txnamt", MoneyUtil.fenToYuan(orderInfo.getMoney()+""));
        dataMap.put("paymentchannel","1");
        dataMap.put("platform",PLATFORM);
        dataMap.put("auth_code",orderInfo.getAliAuthCode());
        dataMap.put("usrmp",userRecipientInfo.getChannelAccount());
        dataMap.put("CUST_ID",cust_id);
        dataMap.put("remark","");
        url = "https://yyfweb.postar.cn/postalpay/p/payAli";
        logger.info("下单URL："+url);
        StringBuilder buf = new StringBuilder("");
        HttpUtils.buildPayParams(buf, dataMap);
        String preStr = buf.toString();
        logger.info("请求参数："+preStr);

        headerMap.put("cookie",httpResponse.getCookie()+"A&3198A896609760CCC83471B2EB998A2B="+orderInfo.getAliUserId());

        String payHttpResponse = HttpUtils.sendPostRequest(url,preStr,headerMap);

        logger.info("星驿付请求返回的:"+payHttpResponse);

        JSONObject payInforesultObj = JSONObject.parseObject(payHttpResponse);
        String tradeNO = payInforesultObj.getString("tradeNO");
        if(StringUtils.isBlank(tradeNO)){
            jsonObject.put("code","100");
            jsonObject.put("msg","获取连接失败");
            return jsonObject;
        }
        jsonObject.put("payUrl",tradeNO);
        jsonObject.put("orderNo",tradeNO);
        jsonObject.put("code","200");
        return jsonObject;
    }
    @Override
    public ResponseData smartReissueOrder(TOrderInfo orderInfo) throws Exception {
        return new ResponseData();
    }

}
